package com.digitalchina.lxnsy.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.PlantProductionDetailActivity;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PlanProductVO;
import com.digitalchina.lxnsy.interfaces.IRecyclerType;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.viewholder.PhenologicalHolder;
import com.digitalchina.lxnsy.viewholder.PlantChildHolder;
import com.digitalchina.lxnsy.viewholder.PlantChildHolderV2;
import com.digitalchina.lxnsy.viewholder.PlantFatherHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2018/8/24 16:01
 * 修改备注：
 * ================================================
 */
public class PlantProductionFragment extends BaseV4Fragment {

    @Bind(R.id.recyclerView1)
    EasyRecyclerView recyclerView1;
    @Bind(R.id.recyclerView2)
    EasyRecyclerView recyclerView2;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_time)
    TextView tvTime;

    private RecyclerArrayAdapter adapter, adapter2;

    private String planId = "";
    private PlanProductVO planProductVO;
    //    private List<IRecyclerType> allList = new ArrayList<>();
    private List<PlanProductVO.FarmOperationBean> farmList = new ArrayList<>();

    //    private MyAdapter myAdapter;
    private int curPhaseMsgInfoIndex = 0;

    // 物候期
    private List<PlanProductVO.ProductStandardBean.PhaseMsgInfosBean> whList = new ArrayList<>();

    private List<Integer> index = new ArrayList<>();

    private String phaseName = "";

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_product_plan);
    }

    @Override
    public void initMember() {

        planId = getArguments().getString("planId");

        BusinessPlan.productionShow(getActivity(), planId, mHandler);

        //水平切换
        recyclerView1.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView1.setAdapter(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new PhenologicalHolder(parent);
            }
        });

        recyclerView2.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView2.setAdapter(adapter2 = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new PlantChildHolderV2(parent);
            }
        });

    }

    public void updatePlanId(String planId) {
        this.planId = planId;
        BusinessPlan.productionShow(getActivity(), planId, mHandler);
    }

    @Override
    public void addListener() {

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                phaseName = whList.get(position).getPhaseName();

                tvName.setText(phaseName);
                tvTime.setText(whList.get(position).getStartTime());

                for (int i = 0; i < whList.size(); i++) {
                    whList.get(i).setChecked(i == position);
                }
                adapter.notifyDataSetChanged();

                farmList.clear();
                adapter2.clear();

                boolean b = true; // 标记当前农事操作之前的都为绿色小圆点
                if (whList.get(position).getFarmOperation() != null) {
                    int size = whList.get(position).getFarmOperation().size();
                    if (position > curPhaseMsgInfoIndex)
                        b = false;
                    for (int j = 0; j < size; j++) {
                        PlanProductVO.FarmOperationBean vo = whList.get(position).getFarmOperation().get(j);
                        vo.setDotColor(b);
                        if (vo.getId().equals(planProductVO.getFarmOperation().getId())) {
                            vo.setCurOper(true);
                            b = false;
                        } else {
                            vo.setCurOper(false);
                        }

                        if (size == 1)
                            vo.setLine(3);
                        else {
                            if (j == 0)
                                vo.setLine(0);
                            else if (j == size - 1)
                                vo.setLine(1);
                            else
                                vo.setLine(2);
                        }

                        farmList.add(vo);
                    }
                }

                adapter2.addAll(farmList);
            }
        });

        adapter2.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                PlanProductVO.FarmOperationBean vo = (PlanProductVO.FarmOperationBean) adapter2.getItem(position);
                vo.setPlanId(planId);
                vo.setPhaseName(phaseName);
                vo.setProductStandardId(planProductVO.getProductStandard().getId());
                vo.setOperation(vo.getId().equals(planProductVO.getFarmOperation().getId()));
                Intent intent = new Intent(getActivity(), PlantProductionDetailActivity.class);
                intent.putExtra("vo", (Serializable) vo);
                startActivityForResult(intent, 97);
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (getActivity() == null || getActivity().isFinishing())
                    return;

                switch (msg.what) {
                    case MSG.MSG_PRODUCTION_SHOW_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PRODUCTION_SHOW_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
            BusinessPlan.productionShow(getActivity(), planId, mHandler);
    }

    @Override
    public void callBack(Object object) {

        adapter.clear();
        adapter2.clear();
        farmList.clear();

        planProductVO = FastJsonUtil.getBean(object.toString(), "productPlan", PlanProductVO.class);

        if (planProductVO != null && planProductVO.getProductStandard() != null) {

            whList = planProductVO.getProductStandard().getPhaseMsgInfos();

            int cur = 0;
            for (int i = 0; i < whList.size(); i++) {
                if (planProductVO.getPhaseMsgInfo().getId().equals(whList.get(i).getId())) {
                    curPhaseMsgInfoIndex = i;
                    whList.get(i).setChecked(true);
                    cur = i;
                    phaseName = whList.get(i).getPhaseName();
                    tvName.setText(phaseName);
                    tvTime.setText(whList.get(i).getStartTime());
                    boolean b = true; // 标记当前农事操作之前的都为绿色小圆点
                    if (whList.get(i).getFarmOperation() != null) {
                        int size = whList.get(i).getFarmOperation().size();
                        for (int j = 0; j < size; j++) {
                            PlanProductVO.FarmOperationBean vo = whList.get(i).getFarmOperation().get(j);
                            vo.setDotColor(b);
                            if (vo.getId().equals(planProductVO.getFarmOperation().getId())) {
                                vo.setCurOper(true);
                                b = false;
                            } else {
                                vo.setCurOper(false);
                            }


                            if (size == 1)
                                vo.setLine(3);
                            else {
                                if (j == 0)
                                    vo.setLine(0);
                                else if (j == size - 1)
                                    vo.setLine(1);
                                else
                                    vo.setLine(2);
                            }


                            farmList.add(vo);
                        }
                    }
                } else
                    whList.get(i).setChecked(false);
            }
            adapter.addAll(whList);
            recyclerView1.scrollToPosition(cur);
            adapter2.addAll(farmList);
        }

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    public class MyAdapter extends RecyclerArrayAdapter<IRecyclerType> {


        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

            if (viewType == 0) {
                return new PlantFatherHolder(parent);
            } else {
                return new PlantChildHolder(parent);
            }
        }

        @Override
        public int getViewType(int position) {

            if (position == 0) {
                return 0;
            } else {
                return getItem(position).type();
            }
        }

    }
}
